﻿using System;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Web.UI;
using System.Text;
using System.Linq;

namespace Evpo.ContactData.CustomControls
{
    public class MultiselectLookupView : Table, ILookupControl
    {
        public MultiselectLookupView()
        {
            SelectedEntries = new List<int>();
            Entries = new List<MultiselectLookupViewEntry>();
        }

        #region Properties
        public List<MultiselectLookupViewEntry> Entries
        {
            get;
            set;
        }

        public string LookupName
        {
            get;
            set;
        }

        public bool Multiselect
        {
            get { return true; }
        }

        public List<int> SelectedEntries
        {
            get;
            set;
        }
        #endregion

        #region Fields
        private LookupTools _lookupTools;
        #endregion

        #region Handlers
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Page.RegisterRequiresControlState(this);
            _lookupTools = new LookupTools(this);
            _lookupTools.GetSelectedItems += new SelectedItemsEventHandler(LookupTools_GetSelectedItems);
            _lookupTools.SetSelectedItems += new SelectedItemsEventHandler(LookupTools_SetSelectedItems);
        }

        void LookupTools_SetSelectedItems(object sender, SelectedItemsEventArgs e)
        {
            if (!e.LookupName.Equals(LookupName, StringComparison.CurrentCultureIgnoreCase))
                return;

            e.IsLookupFound = true;
            
            SelectedEntries = e.SelectedItems;
        }

        private void LookupTools_GetSelectedItems(object sender, SelectedItemsEventArgs e)
        {
            if (!e.LookupName.Equals(LookupName, StringComparison.CurrentCultureIgnoreCase))
                return;

            e.IsLookupFound = true;
            
            e.SelectedItems = SelectedEntries;
        }

        protected override object SaveControlState()
        {
            Pair pair = new Pair(
                    base.SaveControlState(), 
                    new Pair(SelectedEntries, Entries));
            return pair;
        }

        protected override void LoadControlState(object savedState)
        {
            Pair pair = (Pair)savedState;
            base.LoadControlState(pair.First);
            pair = (Pair)pair.Second;
            SelectedEntries = (List<int>)pair.First;
            Entries = (List<MultiselectLookupViewEntry>)pair.Second;
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            this.Rows.Clear();

            var entries = from entry in Entries
                          where SelectedEntries.Contains(Int32.Parse(entry.Value))
                          select entry.Text;

            foreach (string s in entries)
            {
                TableRow row = new TableRow();
                TableCell cell = new TableCell()
                {
                    Text = s
                };

                row.Cells.Add(cell);
                Rows.Add(row);
            }
        }

        #endregion Handlers

        #region Methods
        #endregion Methods

        #region Classes
        
        #endregion Classes
    }

    [Serializable]
    public class MultiselectLookupViewEntry
    {
        public string Text
        {
            get;
            set;
        }

        public string Value
        {
            get;
            set;
        }
    }
}
